package com.furtherstar.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.furtherstar.auth.controller.dto.AppUserQuery;
import com.furtherstar.auth.controller.dto.UserRoleSetting;
import com.furtherstar.auth.dao.mapper.AppRoleMapper;
import com.furtherstar.auth.dao.mapper.CorpWorkMapper;
import com.furtherstar.auth.dao.mapper.UserRoleMapper;
import com.furtherstar.auth.dao.model.AppRole;
import com.furtherstar.auth.dao.model.AppRoleExample;
import com.furtherstar.auth.dao.model.CorpWork;
import com.furtherstar.auth.dao.model.UserRole;
import com.furtherstar.auth.dao.model.UserRoleExample;
import com.furtherstar.auth.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.lang.IDGenerator;
import com.yummy.web.security.bean.SimpleUser;

@Service
public class UserRoleServiceImpl implements UserRoleService{
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private CorpWorkMapper corpWorkMapper;
	@Autowired
	private AppRoleMapper appRoleMapper;

	@Override
	@Transactional
	public Boolean setUserRoles(UserRoleSetting userRoleSetting) {
		CorpWork work = getCorpWork(userRoleSetting.getWorkId());
		AssertUtil.notNull(work);
		deleteUserRolesByWork(work.getId());
		if(CollectionUtils.isEmpty(userRoleSetting.getRoleIds())) {
			return true;
		}
		List<AppRole> roles = getAppRoles(userRoleSetting.getRoleIds());
		List<UserRole> userRoles = extractUserRoles(work,roles);
		if(!CollectionUtils.isEmpty(userRoles)) {
			userRoleMapper.insertBatch(userRoles);
		}
		return true;
	}

	private List<UserRole> extractUserRoles(CorpWork work, List<AppRole> roles) {
		return roles.stream().map(r -> {
			UserRole ur = new UserRole();
			ur.setAppname(r.getAppname());
			ur.setCorpId(work.getCorpId());
			ur.setId(IDGenerator.getId());
			ur.setNickname(work.getNickname());
			ur.setPositionId(work.getPositionId());
			ur.setRoleId(r.getId());
			ur.setUserid(work.getUserid());
			ur.setUsername(work.getUsename());
			ur.setWorkId(work.getId());
			return ur;
		}).toList();
	}

	private List<AppRole> getAppRoles(List<Long> roleIds) {
		AppRoleExample example = new AppRoleExample();
		example.createCriteria().andIdIn(roleIds);
		return appRoleMapper.selectByExample(example);
	}

	private void deleteUserRolesByWork(Long workId) {
		UserRoleExample example = new UserRoleExample();
		example.createCriteria().andWorkIdEqualTo(workId);
		userRoleMapper.deleteByExample(example);
	}

	private CorpWork getCorpWork(Long workId) {
		return corpWorkMapper.selectByPrimaryKey(workId);
	}

	@Override
	public PageSerializable<SimpleUser> queryPage(AppUserQuery appUserQuery) {
		PageHelper.startPage(appUserQuery.getPageNum(), appUserQuery.getPageSize());
		return PageSerializable.of(userRoleMapper.queryDistinctUsers(appUserQuery));
	}

}
